package du.zhaofeng2019;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListViewActivity extends ListActivity {


    String TAG = PackageManager.class.getSimpleName();
    TreeViewAdapter treeViewAdapter;
    ArrayList<TreeElement> nodeList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        nodeList = new TreeDataProvider(this).getDataSource();

//  说明： 数据在 欢迎页面中准备，这里多余
        //如果数据库为空，则读 网络 或 xml
//        if (nodeList.size()==0){
//            new DataStore(ListViewActivity.this);
//            nodeList = new TreeDataProvider(this).getDataSource();
//        }


        ListView listView = getListView();


        listView.setPadding(10, 50, 10, 50);
        listView.setBackgroundColor(Color.rgb(234, 234, 234));

        // nodeList 数据正确的
        treeViewAdapter = new TreeViewAdapter(this, R.layout.activity_splash, nodeList);

        listView.setAdapter(treeViewAdapter);

        registerForContextMenu(getListView());

    }


    private class TreeViewAdapter extends ArrayAdapter {

        private LayoutInflater inflater;
        private ArrayList<TreeElement> treeElementArrayList;
        private Bitmap mIconCollapse;
        private Bitmap mIconExpand;


        public TreeViewAdapter(@NonNull Context context, int resource, @NonNull List objects) {
            super(context, resource, objects);

            inflater = LayoutInflater.from(context);
            treeElementArrayList = (ArrayList<TreeElement>) objects;
            mIconCollapse = BitmapFactory.decodeResource(context.getResources(), R.drawable.right);
            mIconExpand = BitmapFactory.decodeResource(context.getResources(), R.drawable.down);
        }


        @Override
        public int getCount() {
            return treeElementArrayList.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Nullable
        @Override
        public TreeElement getItem(int position) {
            return treeElementArrayList.get(position);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder;
            convertView = inflater.inflate(R.layout.item_contact_detail, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.treetext);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);

            final TreeElement obj = treeElementArrayList.get(position);


            holder.text.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //                 Log.i("TreeView", "obj.id:" + obj.getId());
                    //              obj.forward(ListViewActivity.this);

                    final InfoBean info = new SqlMethod(ListViewActivity.this).findInfoBeanById(Integer.valueOf(obj.getId()));

                    //  : 2019/1/6

                    LinearLayout linearLayout = new LinearLayout(ListViewActivity.this);
                    LinearLayout.LayoutParams layoutParams =
                            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                    LinearLayout.LayoutParams.WRAP_CONTENT);

                    layoutParams.setMargins(66, 66, 66, 66);
                    linearLayout.setLayoutParams(layoutParams);
                    linearLayout.setOrientation(LinearLayout.VERTICAL);
                    linearLayout.setPadding(66, 66, 66, 66);


                    //
                    //
                    //
                    //
                    //
                    //
                    //
                    //
                    TextView tvUnit = new TextView(ListViewActivity.this);
                    tvUnit.setText("单位：");
                    linearLayout.addView(tvUnit);
                    if (info.getUnit() != null) {
                        TextView tv2 = new TextView(ListViewActivity.this);
                        tv2.setText(info.getUnit());
                        tv2.setTextSize(18);
                        tv2.setPadding(66, 0, 0, 0);
                        linearLayout.addView(tv2);

                        if (obj.getLevel() == 2) {//只有第3层，即人员层 才能长按修改  0,1,2

                            //长按修改
                            final TextView finalTv = tv2;
                            tv2.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                    InfoBean oldBean = new SqlMethod(ListViewActivity.this).findInfoBeanById(Integer.valueOf(obj.getId()));

                                    AlertDialog.Builder adb = new AlertDialog.Builder(ListViewActivity.this);

                                    final Spinner spinner = new Spinner(ListViewActivity.this);
                                    ArrayList<Map<String, String>> idUnitArrayListMap = new SqlMethod(ListViewActivity.this).findIdUnitArrayListMap();
                                    final SimpleAdapter simpleAdapter = new SimpleAdapter(ListViewActivity.this,
                                            idUnitArrayListMap,
                                            R.layout.item_spinner,
                                            new String[]{SQLHelper.KEY_UNIT},
                                            new int[]{R.id.text_unit_item_spinner});
                                    spinner.setAdapter(simpleAdapter);

                                    String currUnit = oldBean.getUnit();

                                    //正确显示，spinner当前的 单位
                                    for (int i = 0; i < idUnitArrayListMap.size(); i++) {
                                        Map<String, String> map = idUnitArrayListMap.get(i);
                                        if (map.get(SQLHelper.KEY_UNIT).equals(currUnit)) {
                                            spinner.setSelection(i);
// TODO: 2019/1/8   数据更改后， 界面没有 及时同步更新，以后要修正
                                        }
                                    }

                                    adb.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            HashMap<String, String> selectedItem = (HashMap<String, String>) spinner.getSelectedItem();
                                            info.setUnit(selectedItem.get(SQLHelper.KEY_UNIT));
                                            new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                            finalTv.setText(info.getUnit());//回显
                                        }
                                    });

                                    adb.setView(spinner);
                                    adb.show();
                                    return false;
                                }
                            });
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (obj.getLevel() > 0) {
                        TextView tvDept = new TextView(ListViewActivity.this);
                        tvDept.setText("部门：");
                        linearLayout.addView(tvDept);
                        if (info.getDept() != null) {
                            TextView tv2 = new TextView(ListViewActivity.this);
                            tv2.setText(info.getDept());
                            tv2.setTextSize(18);
                            tv2.setPadding(66, 0, 0, 0);
                            linearLayout.addView(tv2);

                            if (obj.getLevel() == 2) {//只有第3层，即人员层 才能长按修改

                                //长按修改
                                final TextView finalTv = tv2;
                                tv2.setOnLongClickListener(new View.OnLongClickListener() {
                                    @Override
                                    public boolean onLongClick(View v) {
                                        InfoBean oldBean = new SqlMethod(ListViewActivity.this).findInfoBeanById(Integer.valueOf(obj.getId()));
                                        AlertDialog.Builder adb = new AlertDialog.Builder(ListViewActivity.this);

                                        final Spinner spinner = new Spinner(ListViewActivity.this);
                                        ArrayList<Map<String, String>> idDeptArrayListMap = new SqlMethod(ListViewActivity.this).findIdDeptArrayListMap(oldBean.getUnit());
                                        final SimpleAdapter simpleAdapter = new SimpleAdapter(ListViewActivity.this,
                                                idDeptArrayListMap,
                                                R.layout.item_spinner,
                                                new String[]{SQLHelper.KEY_DEPT},
                                                new int[]{R.id.text_unit_item_spinner});
                                        spinner.setAdapter(simpleAdapter);

                                        String currDept = oldBean.getDept();

                                        //正确显示，spinner当前的 单位
                                        for (int i = 0; i < idDeptArrayListMap.size(); i++) {
                                            Map<String, String> map = idDeptArrayListMap.get(i);
                                            if (map.get(SQLHelper.KEY_DEPT).equals(currDept)) {
                                                spinner.setSelection(i);
// TODO: 2019/1/8   数据更改后， 界面没有 及时同步更新，以后要修正
                                            }
                                        }

                                        adb.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                HashMap<String, String> selectedItem = (HashMap<String, String>) spinner.getSelectedItem();
                                                info.setDept(selectedItem.get(SQLHelper.KEY_DEPT));
                                                new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                                finalTv.setText(info.getDept());//回显
                                            }
                                        });

                                        adb.setView(spinner);
                                        adb.show();
                                        return false;
                                    }
                                });
                            }
                        }
                    }

                    //
                    //
                    //
                    //
                    //
                    if (info.getName() != null && obj.getLevel() == 2) {
                        TextView tvName = new TextView(ListViewActivity.this);
                        tvName.setText("姓名：");
                        linearLayout.addView(tvName);
                        TextView tv2 = new TextView(ListViewActivity.this);
                        tv2.setText(info.getName());
                        tv2.setTextSize(18);
                        tv2.setPadding(66, 0, 0, 0);
                        linearLayout.addView(tv2);

                        //长按修改
                        final TextView finalTv = tv2;
                        tv2.setOnLongClickListener(new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {

                                AlertDialog.Builder adb = new AlertDialog.Builder(ListViewActivity.this);
                                final EditText editText = new EditText(ListViewActivity.this);
                                editText.setText(info.getName());
                                editText.setSelection(editText.length());//光标定位最后
                                adb.setView(editText);
                                adb.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        info.setName(String.valueOf(editText.getText()));
                                        new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                        treeViewAdapter.notifyDataSetChanged();
                                        finalTv.setText(info.getName());
                                    }
                                });
                                adb.show();
                                return false;
                            }
                        });
                    }


                    //
                    //
                    //
                    //
                    //
                    TextView tvOfficePhones = new TextView(ListViewActivity.this);
                    tvOfficePhones.setText("办公电话：");
                    tvOfficePhones.setTextColor(Color.BLUE);
                    linearLayout.addView(tvOfficePhones);
                    tvOfficePhones.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(ListViewActivity.this);
                            final EditText editText = new EditText(ListViewActivity.this);
                            editText.setHint("办公电话");
                            builder.setView(editText);
                            editText.setInputType(InputType.TYPE_CLASS_NUMBER);//输入法 数字键盘
                            builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    if (info.getOfficePhones() == null)
                                        info.setOfficePhones(new ArrayList<String>());
                                    info.getOfficePhones().add(String.valueOf(editText.getText()));
                                    new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                    treeViewAdapter.notifyDataSetChanged();
                                }
                            });
                            builder.show();
                            return false;
                        }
                    });
                    if (info.getOfficePhones() != null && info.getOfficePhones().size() > 0) {
                        TextView tv2;
                        for (int i = 0; i < info.getOfficePhones().size(); i++) {
                            tv2 = new TextView(ListViewActivity.this);
                            tv2.setText(info.getOfficePhones().get(i));
                            tv2.setTextSize(18);
                            tv2.setPadding(66, 0, 0, 0);
                            linearLayout.addView(tv2);

                            final int finalI = i;

                            //长按修改
                            final TextView finalTv = tv2;
                            tv2.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {

                                    AlertDialog.Builder adb = new AlertDialog.Builder(ListViewActivity.this);
                                    final EditText editText = new EditText(ListViewActivity.this);
                                    editText.setText(info.getOfficePhones().get(finalI));

                                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);//输入法 数字键盘
                                    editText.setSelection(editText.length());//光标定位最后

                                    adb.setView(editText);
                                    adb.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            info.getOfficePhones().set(finalI, String.valueOf(editText.getText()));
                                            new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                            treeViewAdapter.notifyDataSetChanged();
                                            finalTv.setText(info.getOfficePhones().get(finalI));
                                        }
                                    });
                                    adb.show();
                                    return false;
                                }
                            });

                            //短按打电话
                            tv2.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(Intent.ACTION_CALL);
                                    intent.setData(Uri.parse("tel:" + info.getOfficePhones().get(finalI)));
                                    startActivity(intent);
                                }
                            });
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (obj.getLevel() == 2) {
                        TextView tvHomePhones = new TextView(ListViewActivity.this);
                        tvHomePhones.setText("宅电：");
                        tvHomePhones.setTextColor(Color.BLUE);
                        linearLayout.addView(tvHomePhones);
                        tvHomePhones.setOnLongClickListener(new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(ListViewActivity.this);
                                final EditText editText = new EditText(ListViewActivity.this);
                                editText.setHint("住宅电话");
                                builder.setView(editText);
                                editText.setInputType(InputType.TYPE_CLASS_NUMBER);//输入法 数字键盘
                                builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (info.getHomePhones() == null)
                                            info.setHomePhones(new ArrayList<String>());
                                        info.getHomePhones().add(String.valueOf(editText.getText()));
                                        new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                        treeViewAdapter.notifyDataSetChanged();
                                    }
                                });
                                builder.show();
                                return false;
                            }
                        });
                        if (info.getHomePhones() != null && info.getHomePhones().size() > 0) {
                            TextView tv2;
                            for (int i = 0; i < info.getHomePhones().size(); i++) {
                                tv2 = new TextView(ListViewActivity.this);
                                tv2.setText(info.getHomePhones().get(i));
                                tv2.setTextSize(18);
                                tv2.setPadding(66, 0, 0, 0);
                                linearLayout.addView(tv2);

                                final int finalI = i;

                                //长按修改
                                final TextView finalTv = tv2;
                                tv2.setOnLongClickListener(new View.OnLongClickListener() {
                                    @Override
                                    public boolean onLongClick(View v) {

                                        AlertDialog.Builder adb = new AlertDialog.Builder(ListViewActivity.this);
                                        final EditText editText = new EditText(ListViewActivity.this);
                                        editText.setText(info.getHomePhones().get(finalI));

                                        editText.setInputType(InputType.TYPE_CLASS_NUMBER);//输入法 数字键盘
                                        editText.setSelection(editText.length());//光标定位最后

                                        adb.setView(editText);
                                        adb.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                info.getHomePhones().set(finalI, String.valueOf(editText.getText()));
                                                new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                                treeViewAdapter.notifyDataSetChanged();
                                                finalTv.setText(info.getHomePhones().get(finalI));
                                            }
                                        });
                                        adb.show();
                                        return false;
                                    }
                                });

                                //短按打电话
                                tv2.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(Intent.ACTION_CALL);
                                        intent.setData(Uri.parse("tel:" + info.getHomePhones().get(finalI)));
                                        startActivity(intent);
                                    }
                                });
                            }
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (obj.getLevel() == 2) {
                        TextView tvMobilePhones = new TextView(ListViewActivity.this);
                        tvMobilePhones.setText("手机：");
                        tvMobilePhones.setTextColor(Color.BLUE);
                        linearLayout.addView(tvMobilePhones);
                        tvMobilePhones.setOnLongClickListener(new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(ListViewActivity.this);
                                final EditText editText = new EditText(ListViewActivity.this);
                                editText.setHint("手机号码");
                                builder.setView(editText);
                                editText.setInputType(InputType.TYPE_CLASS_NUMBER);//输入法 数字键盘
                                builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (info.getMobilePhones() == null)
                                            info.setMobilePhones(new ArrayList<String>());
                                        info.getMobilePhones().add(String.valueOf(editText.getText()));
                                        new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                        treeViewAdapter.notifyDataSetChanged();
                                    }
                                });
                                builder.show();
                                return false;
                            }
                        });
                        if (info.getMobilePhones() != null && info.getMobilePhones().size() > 0) {
                            TextView tv2;
                            for (int i = 0; i < info.getMobilePhones().size(); i++) {
                                tv2 = new TextView(ListViewActivity.this);
                                tv2.setText(info.getMobilePhones().get(i));
                                tv2.setTextSize(18);
                                tv2.setPadding(66, 0, 0, 0);
                                linearLayout.addView(tv2);

                                final int finalI = i;

                                //长按修改
                                final TextView finalTv = tv2;
                                tv2.setOnLongClickListener(new View.OnLongClickListener() {
                                    @Override
                                    public boolean onLongClick(View v) {

                                        AlertDialog.Builder adb = new AlertDialog.Builder(ListViewActivity.this);
                                        final EditText editText = new EditText(ListViewActivity.this);
                                        editText.setText(info.getMobilePhones().get(finalI));

                                        editText.setInputType(InputType.TYPE_CLASS_NUMBER);//输入法 数字键盘
                                        editText.setSelection(editText.length());//光标定位最后

                                        adb.setView(editText);
                                        adb.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                info.getMobilePhones().set(finalI, String.valueOf(editText.getText()));
                                                new SqlMethod(ListViewActivity.this).updateOrSave(info);
                                                treeViewAdapter.notifyDataSetChanged();
                                                finalTv.setText(info.getMobilePhones().get(finalI));
                                            }
                                        });
                                        adb.show();
                                        return false;
                                    }
                                });

                                //短按打电话
                                tv2.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(Intent.ACTION_CALL);
                                        intent.setData(Uri.parse("tel:" + info.getMobilePhones().get(finalI)));
                                        startActivity(intent);
                                    }
                                });
                            }
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (false) {
                        TextView tvQQs = new TextView(ListViewActivity.this);
                        tvQQs.setText("QQ：");
                        linearLayout.addView(tvQQs);
                        if (info.getQQs() != null && info.getQQs().size() > 0) {
                            TextView tv2;
                            for (int i = 0; i < info.getQQs().size(); i++) {
                                tv2 = new TextView(ListViewActivity.this);
                                tv2.setText(info.getQQs().get(i));
                                tv2.setTextSize(18);
                                tv2.setPadding(66, 0, 0, 0);
                                linearLayout.addView(tv2);
                            }
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (false) {
                        TextView tvWeixins = new TextView(ListViewActivity.this);
                        tvWeixins.setText("微信：");
                        linearLayout.addView(tvWeixins);
                        if (info.getWeixins() != null && info.getWeixins().size() > 0) {
                            TextView tv2;
                            for (int i = 0; i < info.getWeixins().size(); i++) {
                                tv2 = new TextView(ListViewActivity.this);
                                tv2.setText(info.getWeixins().get(i));
                                tv2.setTextSize(18);
                                tv2.setPadding(66, 0, 0, 0);
                                linearLayout.addView(tv2);
                            }
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (false) {
                        TextView tvEmails = new TextView(ListViewActivity.this);
                        tvEmails.setText("邮箱：");
                        linearLayout.addView(tvEmails);
                        if (info.getEmails() != null && info.getEmails().size() > 0) {
                            TextView tv2;
                            for (int i = 0; i < info.getEmails().size(); i++) {
                                tv2 = new TextView(ListViewActivity.this);
                                tv2.setText(info.getEmails().get(i));
                                tv2.setTextSize(18);
                                tv2.setPadding(66, 0, 0, 0);
                                linearLayout.addView(tv2);
                            }
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (false) {
                        TextView tvTeams = new TextView(ListViewActivity.this);
                        tvTeams.setText("团队：");
                        linearLayout.addView(tvTeams);
                        if (info.getTeams() != null && info.getTeams().size() > 0) {
                            TextView tv2;
                            for (int i = 0; i < info.getTeams().size(); i++) {
                                tv2 = new TextView(ListViewActivity.this);
                                tv2.setText(info.getTeams().get(i));
                                tv2.setTextSize(18);
                                tv2.setPadding(66, 0, 0, 0);
                                linearLayout.addView(tv2);
                            }
                        }
                    }


                    //
                    //
                    //
                    //
                    //
                    if (false) {
                        TextView tvAddresses = new TextView(ListViewActivity.this);
                        tvAddresses.setText("住址：");
                        linearLayout.addView(tvAddresses);
                        if (info.getAddresses() != null && info.getAddresses().size() > 0) {
                            TextView tv2;
                            for (int i = 0; i < info.getAddresses().size(); i++) {
                                tv2 = new TextView(ListViewActivity.this);
                                tv2.setText(info.getAddresses().get(i));
                                tv2.setTextSize(18);
                                tv2.setPadding(66, 0, 0, 0);
                                linearLayout.addView(tv2);
                            }
                        }
                    }


                    AlertDialog.Builder detailDialog = new AlertDialog.Builder(ListViewActivity.this);
                    detailDialog.setView(linearLayout);
                    detailDialog.show();

                }
            });
            //
            //
            //
            //
            //
            //


            int level = obj.getLevel();
            holder.icon.setPadding(66 * (level + 1), holder.icon.getPaddingTop(), 0, holder.icon.getPaddingBottom());


            if (level == 0)
                holder.text.setTextColor(Color.RED);
            else if (level == 1)
                holder.text.setTextColor(Color.BLACK);
            else
                holder.text.setTextColor(Color.BLUE);


            holder.text.setText(obj.getTitle());
            if (obj.isHasChild() && (obj.isExpanded() == false)) {
                holder.icon.setImageBitmap(mIconCollapse);
            } else if (obj.isHasChild() && (obj.isExpanded() == true)) {
                holder.icon.setImageBitmap(mIconExpand);
            } else if (!obj.isHasChild()) {
                holder.icon.setImageBitmap(mIconCollapse);
                holder.icon.setVisibility(View.INVISIBLE);
            }


            return convertView;
        }

        class ViewHolder {
            ImageView icon;
            TextView text;
        }

    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
//        Log.i("TreeView", "position:" + position);
        if (!nodeList.get(position).isHasChild()) {
            // ? 什么作用？
//            Toast.makeText(this, nodeList.get(position).getTitle(), Toast.LENGTH_LONG);
            return;
        }

        if (nodeList.get(position).isExpanded()) {
            nodeList.get(position).setExpanded(false);
            TreeElement element = nodeList.get(position);
            ArrayList<TreeElement> temp = new ArrayList<TreeElement>();

            for (int i = position + 1; i < nodeList.size(); i++) {
                if (element.getLevel() >= nodeList.get(i).getLevel()) {
                    break;
                }
                temp.add(nodeList.get(i));
            }

            nodeList.removeAll(temp);

            treeViewAdapter.notifyDataSetChanged();

        } else {
            TreeElement obj = nodeList.get(position);
            obj.setExpanded(true);
            int level = obj.getLevel();
            int nextLevel = level + 1;

            for (TreeElement element : obj.getChilds()) {
                element.setLevel(nextLevel);
                element.setExpanded(false);
                nodeList.add(position + 1, element);
            }
            treeViewAdapter.notifyDataSetChanged();
        }
    }


}
